package com.cskaoyan.controller;

import com.cskaoyan.bean.BO.AdminTopicBO;
import com.cskaoyan.bean.PO.MarketTopic;
import com.cskaoyan.bean.VO.AdminTopicReadVO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminTopicService;
import com.cskaoyan.utils.TypeConverter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wenkaikai
 * @since 2022/08/14 15:37
 */

@RequestMapping("admin/topic")
@RestController
public class AdminTopicController {

    @Autowired
    AdminTopicService adminTopicService;


    /**
     * 专题列表
     * @param info
     * @param title    根据专题 标题搜索
     * @param subtitle 根据专题 子标题搜索
     * @return
     */
    @RequiresPermissions("admin:topic:list")
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, String title, String subtitle) {
        CommonData<MarketTopic> data = adminTopicService.queryTopicList(info, title, subtitle);
        return BaseRespVo.ok(0, data, "成功");
    }

    /**
     * 创建标题，出现goods 数组，MarketTopic接受不到，只能新建BO
     * @param adminTopicBO
     * @return
     */
    @RequiresPermissions("admin:topic:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody AdminTopicBO adminTopicBO) {
        // todo:
        // 有些参数要进行非法判断
        AdminTopicBO data = adminTopicService.createTopic(adminTopicBO);
        return BaseRespVo.ok(0, data, "成功");
    }

    /**
     *  编辑时数据回显
     * @param id
     * @return
     */
    @RequiresPermissions("admin:topic:read")
    @RequestMapping("read")
    public BaseRespVo read(Integer id) {
        AdminTopicReadVO data = adminTopicService.queryTopicById(id);
        return BaseRespVo.ok(0, data, "成功");
    }

    /**
     * 编辑前要先回显，read
     * @param marketTopic
     * @return
     */
    @RequiresPermissions("admin:topic:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketTopic marketTopic) {
        // todo:
        // 有些参数要进行非法判断
        MarketTopic topic = adminTopicService.updateTopic(marketTopic);
        return BaseRespVo.ok(0,topic,"成功");
    }

    /**
     * 逻辑删除
     * @return
     */
    @RequiresPermissions("admin:topic:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketTopic marketTopic) {
        // 先判断它是否就是删除状态
        if(marketTopic.getDeleted()){
            return BaseRespVo.ok(400, null, "该专题已删除，无法重复删除！");
        }
        int code = adminTopicService.deleteTopicLogic(marketTopic);
        if(code == 1) {
            return BaseRespVo.ok(0, null, "成功");
        }
        return BaseRespVo.ok(400, null, "删除失败");
    }

    /**
     * 批量删除,
     * 不做判断了，因为体验不行，直接把选中的所有专题deleted直接全设置为true
     * 即使原来就是true 也再设置一遍
     */
    @RequiresPermissions("admin:topic:batch-delete")
    @RequestMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody String ids) {
        String[] split = ids.split(":");
        String idsStr = split[split.length - 1].replace("}", "");
        Integer[] idsArr = TypeConverter.stringToIntegerArray(idsStr);
        if (idsArr == null || idsArr.length == 0){
            return BaseRespVo.ok(400, null, "参数不能为空！");
        }
        int code = adminTopicService.batchDeleteTopics(idsArr);
        if (code > 0) {
            return BaseRespVo.ok(0, null, "成功");
        }
        return BaseRespVo.ok(400, null, "批量删除失败");
    }

}